﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using System.Text.RegularExpressions;

namespace ChinaDynastyTours.Views
{
    public partial class TravelEvaluationForm : DevExpress.XtraEditors.XtraForm
    {
        public TravelEvaluationForm()
        {
            InitializeComponent();
        }

        private void ServiceEvaluationForm_Load(object sender, EventArgs e)
        {
            tbTravels.Properties.DataSource = BaseInfoCtrl.GetProviderData(new ProviderTypeEnum[] { ProviderTypeEnum.旅行社});
            tbFromDate.DateTime = DateTime.Now;
            tbToDate.DateTime = DateTime.Now.AddDays(7);
            tbFromDate2.DateTime = DateTime.Now;
            tbToDate2.DateTime = DateTime.Now.AddDays(7);
        }

        private void btnStatistics_Click(object sender, EventArgs e)
        {
            if (tbTravels.EditValue != null)
            {
                string travelId  = tbTravels.EditValue.ToString();
                feedbackGridControl.DataSource = FeedbackCtrl.GetFeedbackStatisticsDataForTravel(travelId, tbFromDate.Text,tbToDate.Text);
            }
        }

        private void feedbackGridView_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.CellValue!=null && Regex.IsMatch(e.CellValue.ToString(), @"^\d+$"))//是数字字符串
            {
                float value = float.Parse(e.CellValue.ToString());
                if (value <= 6)
                {
                    e.Appearance.BackColor = Color.FromArgb(192, 0, 0);
                    e.Appearance.BackColor2 = Color.LightGreen;
                }
            }
        }

        private void btnStatistics1_Click(object sender, EventArgs e)
        {
            DataTable dt = FeedbackCtrl.GetStatisticsGroupForTravel(tbFromDate2.Text,tbToDate2.Text);
            List<FeedbackStatisticsEntity> Entities = new List<FeedbackStatisticsEntity>();
            DataTable dtStatisticsAverage;

            //为旅行社逐个创建评价Entity
            foreach (DataRow dr in dt.Rows)
            {
                dtStatisticsAverage = FeedbackCtrl.GetStatisticsGroupAverageForTravel(dr["ProviderID"].ToString(), tbFromDate2.Text,tbToDate2.Text);

                //创建一个旅行社的评价信息实体
                FeedbackStatisticsEntity entity = new FeedbackStatisticsEntity()
                {
                    //TourgroupID = _TourgroupID,
                    //TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourgroupID),
                    ProviderID = int.Parse(dr["ProviderID"].ToString()),
                    ProviderName = BaseInfoCtrl.GetTeamProviderNameById(dr["ProviderID"].ToString()),
                    Vehicle = float.Parse(dtStatisticsAverage.Rows[0]["AverageVehicle"].ToString()),
                    Restaurant = float.Parse(dtStatisticsAverage.Rows[0]["AverageRestaurant"].ToString()),
                    Park = float.Parse(dtStatisticsAverage.Rows[0]["AveragePark"].ToString()),
                    Shop = float.Parse(dtStatisticsAverage.Rows[0]["AverageShop"].ToString()),
                    Guide = float.Parse(dtStatisticsAverage.Rows[0]["AverageGuide"].ToString()),
                    //Accompany = float.Parse(dtStatisticsAverage.Rows[0]["AverageAccompany"].ToString()),
                    Hotel = float.Parse(dtStatisticsAverage.Rows[0]["AverageHotel"].ToString()),
                };
                Entities.Add(entity);
            }

            feedbackGridControl1.DataSource = Entities;
        }
    }
}
